package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.WallpaperDetailActivity;
import flc.ast.adapter.WallpaperAdapter;
import flc.ast.databinding.FragmentWallpaperBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link WallpaperFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class WallpaperFragment extends BaseNoModelFragment<FragmentWallpaperBinding> {

    private WallpaperAdapter choicenessWallpaperAdapter;
    private WallpaperAdapter latestWallpaperAdapter;
    private WallpaperAdapter hotWallpaperAdapter;
    private int type = 0;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.fragment_wallpaper;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.ivChoiceness.setOnClickListener(this);
        mDataBinding.ivLatest.setOnClickListener(this);
        mDataBinding.ivHot.setOnClickListener(this);

        mDataBinding.rvWallpaperList.setLayoutManager(new GridLayoutManager(mContext, 2));
        choicenessWallpaperAdapter = new WallpaperAdapter();
        mDataBinding.rvWallpaperList.setAdapter(choicenessWallpaperAdapter);
        choicenessWallpaperAdapter.addChildClickViewIds(R.id.ivWallpaperItemLike);
        choicenessWallpaperAdapter.setOnItemClickListener(this);
        choicenessWallpaperAdapter.setOnItemChildClickListener(this);

        latestWallpaperAdapter = new WallpaperAdapter();
        latestWallpaperAdapter.addChildClickViewIds(R.id.ivWallpaperItemLike);
        latestWallpaperAdapter.setOnItemClickListener(this);
        latestWallpaperAdapter.setOnItemChildClickListener(this);

        hotWallpaperAdapter = new WallpaperAdapter();
        hotWallpaperAdapter.addChildClickViewIds(R.id.ivWallpaperItemLike);
        hotWallpaperAdapter.setOnItemClickListener(this);
        hotWallpaperAdapter.setOnItemChildClickListener(this);

        mDataBinding.refreshTabItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshTabItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshTabItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (type == 0) {
                    getChoiceData();
                } else if (type == 1) {
                    getLatestData();
                } else if (type == 2) {
                    getHotData();
                }
                mDataBinding.refreshTabItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if (type == 0) {
                    getChoiceData();
                } else if (type == 1) {
                    getLatestData();
                } else if (type == 2) {
                    getHotData();
                }
                mDataBinding.refreshTabItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivChoiceness://精选
                clearSelection();
                type = 0;
                mDataBinding.ivChoiceness.setImageResource(R.drawable.bt_jx);
                mDataBinding.rvWallpaperList.setAdapter(choicenessWallpaperAdapter);
                break;
            case R.id.ivLatest://最新
                clearSelection();
                type = 1;
                mDataBinding.ivLatest.setImageResource(R.drawable.bt_zx);
                mDataBinding.rvWallpaperList.setAdapter(latestWallpaperAdapter);
                break;
            case R.id.ivHot://最热
                clearSelection();
                type = 2;
                mDataBinding.ivHot.setImageResource(R.drawable.bq_zr);
                mDataBinding.rvWallpaperList.setAdapter(hotWallpaperAdapter);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        WallpaperDetailActivity.wallpaperBean = bean;
        startActivity(WallpaperDetailActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivWallpaperLike://点赞
                List<StkResBean> listSp = AppSpUtils.getLikeList();
                StkResBean bean = (StkResBean) adapter.getItem(position);
                if (bean.isSelected()) {
                    listSp.remove(bean);
                    AppSpUtils.saveLikeList(listSp);
                    bean.setSelected(false);
                } else {
                    List<StkResBean> listSave = new ArrayList<>();
                    bean.setSelected(true);
                    listSave.add(bean);
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveLikeList(listSp);
                    } else {
                        AppSpUtils.saveLikeList(listSave);
                    }
                }
                adapter.notifyItemChanged(position);
                break;
        }
    }

    @Override
    protected void initData() {
        getChoiceData();
        getLatestData();
        getHotData();
    }

    @Override
    public void onResume() {
        super.onResume();
        choicenessWallpaperAdapter.notifyDataSetChanged();
        latestWallpaperAdapter.notifyDataSetChanged();
        hotWallpaperAdapter.notifyDataSetChanged();
    }

    private void getChoiceData() {
        StkResApi.getTagResourceList(this, AppConfig.CHOICE_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            choicenessWallpaperAdapter.setList(result);
                        } else {
                            choicenessWallpaperAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getLatestData() {
        StkResApi.getTagResourceList(this, AppConfig.LATEST_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            latestWallpaperAdapter.setList(result);
                        } else {
                            latestWallpaperAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getHotData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            hotWallpaperAdapter.setList(result);
                        } else {
                            hotWallpaperAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void clearSelection() {
        mDataBinding.ivChoiceness.setImageResource(R.drawable.bq_jxa);
        mDataBinding.ivLatest.setImageResource(R.drawable.bq_zxa);
        mDataBinding.ivHot.setImageResource(R.drawable.bq_zra);
    }

}